﻿using CommonUtil;
using CommonUtil.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace view
{
    public partial class SettingFrm : Form
    {
        /// <summary>
        /// 构造方法
        /// </summary>
        public SettingFrm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 画面初期化
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void SettingFrm_Load(object sender, EventArgs e)
        {
            var appInfo = ConfigManager.getConfigInfo();

            // 人脸API
            txtFaceApiKey.Text = appInfo.FaceApiKey;
            // 情感API
            txtEmotionApiKey.Text = appInfo.EmotionApiKey;
            // 是否开启语言播报（0:不开启,1:开启）
            if (appInfo.SpeechEnabled)
                rdoIsSpeech.Checked = true;
            else
                rdoIsNotSpeech.Checked = true;
            // 人脸跟踪
            if (appInfo.Facetracking)
                rdoFacetracking.Checked = true;
            else
                rdoNoFacetracking.Checked = true;
            // EmguCV人脸识别类型(0:基于PCA变换的人脸识别 1:基于Fisher变换的人脸识别 2:基于局部二值模式的人脸识别)
            cmbFaceRecognizerType.SelectedIndex = int.Parse(appInfo.FaceRecognizerType);
            // 自动识别间隔时间（秒）
            txtIdentifyTime.Value = decimal.Parse(appInfo.IdentifyTime);
        }

        /// <summary>
        /// 确认按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOK_Click(object sender, EventArgs e)
        {
            var appInfo = ConfigManager.getConfigInfo();

            // 人脸API
            appInfo.FaceApiKey = txtFaceApiKey.Text;
            // 情感API
            appInfo.EmotionApiKey = txtEmotionApiKey.Text;
            // 是否开启语言播报（0:不开启,1:开启）
            appInfo.SpeechEnabled = rdoIsSpeech.Checked;
            // 人脸跟踪
            appInfo.Facetracking = rdoFacetracking.Checked;
            // EmguCV人脸识别类型(0:基于PCA变换的人脸识别 1:基于Fisher变换的人脸识别 2:基于局部二值模式的人脸识别)
            appInfo.FaceRecognizerType = cmbFaceRecognizerType.SelectedIndex.ToString();
            // 自动识别间隔时间（秒）
            appInfo.IdentifyTime = txtIdentifyTime.Value.ToString();

            // 更新配置信息
            ConfigManager.SetConfigInfo(appInfo);

            this.Close();
        }

        /// <summary>
        /// 取消按钮事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        /// <summary>
        /// 控制只能输入数字
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtIdentifyTime_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 0x20) e.KeyChar = (char)0;  //禁止空格键  
            if ((e.KeyChar == 0x2D) && (((TextBox)sender).Text.Length == 0)) return;   //处理负数  
            if (e.KeyChar > 0x20)
            {
                try
                {
                    double.Parse(((TextBox)sender).Text + e.KeyChar.ToString());
                }
                catch
                {
                    e.KeyChar = (char)0;   //处理非法字符  
                }
            }
        }
    }
}
